package xyz.jcat.docker.example.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.jcat.common.web.Resp;
import xyz.jcat.common.web.RespCodeMsg;
import xyz.jcat.docker.example.domain.po.TestPo;
import xyz.jcat.docker.example.service.ITestService;

import java.util.List;
import java.util.Objects;

@RestController
public class TestController {

    @Autowired
    private ITestService testService;

    @PostMapping("/test")
    public Resp addTest(String name) {
        testService.addTest(name);
        return Resp.ok();
    }

    @GetMapping("/tests")
    public Resp getTests() {
        List<TestPo> tests = testService.getTests();
        return Resp.ok(tests);
    }

    @GetMapping("/test/{id}")
    public Resp getTest(@PathVariable Long id) {
        TestPo test = testService.getById(id);
        if(Objects.isNull(test))
            return Resp.fail(RespCodeMsg.DATA_NOT_FOUND);
        return Resp.ok(test);
    }
}
